package com.hy.ruiji.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hy.ruiji.common.customException;
import com.hy.ruiji.dto.SetmealDto;
import com.hy.ruiji.enyity.Setmeal;
import com.hy.ruiji.enyity.SetmealDish;
import com.hy.ruiji.mapper.SetMealMapper;
import com.hy.ruiji.service.SetMealService;
import com.hy.ruiji.service.SetmealDishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetMealServiceImpl extends ServiceImpl<SetMealMapper, Setmeal> implements SetMealService {
    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 保存套餐
     * @param setmealDto 保存的信息
     */
    @Override
    @Transactional//保证事务的一致性
    public void saveWithDish(SetmealDto setmealDto) {
        //保存套餐的基本信息
        this.save(setmealDto);
        //保存套餐和菜品的关联信息
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().map((item)->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 对菜品状态的修改
     * @param status 需要更改的状态
     * @param ids 需要更改的套餐id数组
     */
    @Override
    @Transactional
    public void updateStatus(Integer status, Long[] ids) {
        if(ids ==null|| ids.length==0){
            throw new customException("数据丢失，请稍后再试！");
        }
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Setmeal::getId,ids);
        //修改状态
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        this.update(setmeal,wrapper);
    }

    /**
     * 根据id回显套餐信息
     * @param id 回显信息的id
     * @return 返回回显的信息
     */
    @Override
    public SetmealDto queryById(Long id) {
        //查询套餐的基本信息
        Setmeal setmeal = this.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        //拷贝信息
        BeanUtils.copyProperties(setmeal,setmealDto);
        //查询套餐和菜品的关联信息
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list = setmealDishService.list(wrapper);
        setmealDto.setSetmealDishes(list);
        return setmealDto;
    }

    /**
     * 更改套餐信息
     * @param setmealDto 需要更改的信息
     */
    @Override
    @Transactional
    public void updateSetMealById(SetmealDto setmealDto) {
        //更新套餐的基本信息
        this.updateById(setmealDto);
        //清理套餐的基本信息
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(wrapper);
        //添加套餐和菜品的关联信息
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().map((item)->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    @Transactional
    public void deleteStMaealDtoByids(Long[] ids) {
        //查询菜品是否在售，在售返回异常
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Setmeal::getId,ids);
        wrapper.eq(Setmeal::getStatus,1);
        int count = this.count(wrapper);
        if(count>0){
            throw new customException("套餐在售，无法删除，请先停售！");
        }
        //删除SetMeal表的信息
        LambdaQueryWrapper<Setmeal> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.in(Setmeal::getId,ids);
        this.remove(wrapper2);
        //删除SetMealDish的信息
        LambdaQueryWrapper<SetmealDish> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.in(SetmealDish::getSetmealId,ids);
        setmealDishService.remove(wrapper1);
    }
}
